using System;
using System.Collections.Generic;
using System.Text;
using Fieldbook.Entities;
using Fieldbook.Common;
using Fieldbook.DAL;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace Fieldbook.Business
{
    public static class StorageController
    {
        public static List<StorageItem> LoadStorageItems(long userID, long categoryID)
        {
            List<StorageItem> res = null;
            try
            {
                res = StorageItemDAO.LaodStorageItems(userID, categoryID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Error loading resource categories", ex);
            }
            return res;
        }

        public static bool InsertStorageTransaction(long userID, long resourceID, decimal pricePerUnit, decimal quota, decimal totaAmount, StorageTranaction.StorageTransactionType transactionType)
        {
            bool res = false;
            DbTransaction transaction = null;
            try
            {
                long? houseHoldID = HouseholdDAO.GetHouseHoldID(userID);
                if (houseHoldID == null) { return false; }
                long? storageID = StorageItemDAO.GetStorageID(resourceID, (long)houseHoldID);
                if (transactionType == StorageTranaction.StorageTransactionType.Sell)
                {
                    quota = quota * -1;
                }
                SqlDatabase db = Utilities.GetDatabase();
                transaction = Utilities.BeginTransaction(db);
                /* insert or update storage */
                if (storageID == null) // insert new storage item
                {
                    StorageItem storageItem = new StorageItem();
                    storageItem.ResourceID = resourceID;
                    storageItem.HouseHoldID = (long)houseHoldID;
                    storageItem.Quota = quota;
                    StorageItemDAO.InsertStorageItem(storageItem, transaction, db);
                } 
                else 
                {
                    StorageItemDAO.UpateStorageQuota((long)storageID, quota, transaction, db);
                }
                /* and logging storage transaction */
                StorageTranaction sTransacton = new StorageTranaction();
                sTransacton.TransactionTypeID = transactionType;
                sTransacton.ResourceID = resourceID;
                sTransacton.OperatorID = userID;
                sTransacton.PricePerUnit = pricePerUnit;
                sTransacton.Units = quota;
                sTransacton.TotalAmount = totaAmount;
                sTransacton.Date = DateTime.Now;
                res = StorageTransactionDAO.InsertStorageTranaction(sTransacton, transaction, db);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null) { transaction.Rollback(); }
                LogManager.LogException("Error inserting storage transaction", ex);
            }
            finally
            {
                Utilities.EndTransaction(transaction);
            }
            return res;
        }
    } 
}
